package cc.rengu.igas.channel.cups.core.service.trans;


import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.TrapRevsalRequest;
import cc.rengu.igas.channel.cups.facade.response.TrapRevsalResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * 指定账户圈存冲正
 *
 * @author who
 */
public class CupsTrapRevsalService extends BasePayService<TrapRevsalRequest, TrapRevsalResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, TrapRevsalRequest trapRevsalRequest, TrapRevsalResponse trapRevsalResponse) throws Exception {
        //  请求
        ConvertUtil.convertOutput(cupsFieldBean);
        CupsField cupsField = cupsFieldBean.getCupsField();
        BeanUtil.beanCopy(trapRevsalRequest, cupsField);
        cupsField.setMsgType("0420");

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        //  特殊计费类型
        cupsFieldBean.createF60()
                .setF60_1("4021")
                .setF60_2_2(trapRevsalRequest.getTermRead())
                .setF60_2_3(trapRevsalRequest.getIcConCode())
                .setF60_2_5(trapRevsalRequest.getTermType());
        cupsFieldBean.build();

        String tmpAmt = AmountUtil.changeY2F(trapRevsalRequest.getTxnAmt());
        cupsField.setOrderAmt(String.format("%012d", Integer.valueOf(tmpAmt)));

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, trapRevsalRequest.getHeader().getTxnNum());

        //  应答
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, trapRevsalResponse);
        trapRevsalResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (CupsParamConstant.SUCCESS_CODE.equals(trapRevsalResponse.getRespCode())) {
            trapRevsalResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            trapRevsalResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(trapRevsalResponse);
    }

}
